import logging
import sys

from insight_agent.agent_rule_new_customer_lijia.group_profile.group_profile_process import GroupProfile
from llm.analyst_agent import AnalystAgent
from llm.analyst_agent_area import AnalystAgentArea
from llm.analyst_agent_group import AnalystAgentGroup
from llm.analyst_agent_stat import AnalystAgentStat
from llm.purpose_agent import PurposeAgent

logging.basicConfig(
    level=logging.INFO,
    filename=f'{sys.path[0]}/log/chat.log',
    filemode='w'
)
"""
处理自然人模拟数据形成群体画像，存入内存、neo4j，并基于数据进行对话

1. 每次对话识别用户意图为以下4个场景之一：
        0.基础对话
        1.探索群体的全局情况
        2.探索群体的局部情况
        3.探索具体的群体
        
2. 识别意图和参数并调用相应方法取数

3. 使用对应场景的智能体进行数据解读
"""
if __name__ == '__main__':
    group_profile = GroupProfile()  # 群体数据准备

    purpose_agent = PurposeAgent()  # 意图识别智能体
    analyst_agent = AnalystAgent()  # 基本对话智能体
    analyst_agent_stat = AnalystAgentStat()  # 群组全局数据分析智能体
    analyst_agent_area = AnalystAgentArea()  # 群组地区数据分析智能体
    analyst_agent_group = AnalystAgentGroup()  # 群组具体数据分析智能体
    history = []  # 对话记录
    """模拟与用户多轮对话"""
    while True:
        prompt = input("请用户输入问题:")
        logging.info("正在识别用户意图")
        data = purpose_agent.purpose_chat(prompt, [])
        purpose = data['purpose']  # 用户意图code （0～3）
        reason = data['reason']  # 用户意图判断依据
        answer = ""
        if purpose == 0:
            logging.info("用户意图识别为:基础对话")
            logging.info(f"识别依据：{reason}")
            answer = analyst_agent.analyst_chat(None, prompt, history)

        elif purpose == 1:
            logging.info("用户意图识别为:探索群体的全局情况")
            logging.info(f"识别依据：{reason}")
            # 查询群体全局信息
            logging.info("即将查询群体统计信息")
            group_info = group_profile.get_statics()
            answer = analyst_agent_stat.analyst_chat(group_info, prompt, history)

        elif purpose == 2:
            logging.info("用户意图识别为:探索群体的局部情况")
            logging.info(f"识别依据：{reason}")
            # 查询群体局部信息
            cities = data['value']
            # 查询群体个别信息
            logging.info("即将查询局部群体信息")
            city_groups = []
            for city in cities:
                city_groups += group_profile.get_group_by_city(city)
            answer = analyst_agent_area.analyst_chat(city_groups, prompt, history)

        elif purpose == 3:
            logging.info("用户意图识别为:探索具体的群体")
            logging.info(f"识别依据：{reason}")
            groupids = data['value']
            # 查询群体个别信息
            logging.info("即将查询具体群体信息")
            groups = []
            for groupid in groupids:
                groups.append(group_profile.get_group(groupid))
            answer = analyst_agent_group.analyst_chat(groups, prompt, history)
            for groupid in groupids:
                group_profile.draw_group(groupid)

        else:
            logging.info("意图识别错误")

        history.append({
            "role": "user",
            "content": prompt
        })
        history.append({
            "role": "assistant",
            "content": answer
        })
